from django.db import models


# 快速回测运行表
class TBackRun(models.Model):
    ID = models.AutoField(
        db_column="ID",
        primary_key=True,
        # default=0
    )
    StgID = models.IntegerField(
        db_column="StgID",
        verbose_name="策略代码"
    )
    BatchID = models.IntegerField(
        db_column="BatchID",
        verbose_name="策略运行代码"
    )
    StartTime = models.CharField(
        db_column="StartTime",
        max_length=19,
        verbose_name="开始运行时间"

    )
    EndTime = models.CharField(
        db_column="EndTime",
        max_length=19,
        verbose_name="结束运行时间"
    )
    Frequency = models.CharField(
        db_column="Frequency",
        max_length=1,
        verbose_name="回测行情频率"
    )
    Region = models.CharField(
        db_column="Region",
        max_length=50,
        verbose_name="回测行情区间"
    )
    RunStatus = models.CharField(
        db_column="RunStatus",
        max_length=1,
        verbose_name="策略进程运作状态"
    )

    class Meta:
        unique_together = (("StgID", "BatchID"),)
        db_table = "t_backrun"


# 快速回测成交表
class TBackTrade(models.Model):
    # ID = models.AutoField(
    #     db_column="ID",
    #
    # )
    StgID = models.IntegerField(
        db_column="StgID",
        verbose_name="策略代码",
        primary_key=True
    )
    BatchID = models.IntegerField(
        db_column="BatchID",
        verbose_name="策略运行代码"
    )
    AccountID = models.CharField(
        db_column="AccountID",
        max_length=32,
        verbose_name="分账号代码"
    )
    Symbol = models.CharField(
        db_column="Symbol",
        max_length=30,
        verbose_name="合约代码"
    )
    TradingDay = models.CharField(
        db_column="TradingDay",
        max_length=8,
        verbose_name="交易日"
    )
    ExchID = models.CharField(
        db_column="ExchID",
        max_length=10,
        verbose_name="交易所代码"
    )
    Direction = models.CharField(
        db_column="Direction",
        max_length=1,
        verbose_name="买卖方向"
    )
    Offset = models.CharField(
        db_column="Offset",
        max_length=1,
        verbose_name="开平仓类型"
    )
    TradePx = models.DecimalField(
        db_column="TradePx",
        max_digits=20,
        decimal_places=2,
        verbose_name="成交价"
    )
    Qty = models.BigIntegerField(
        db_column="Qty",
        verbose_name="成交数量"
    )
    OpenPx = models.DecimalField(
        db_column="OpenPx",
        max_digits=20,
        decimal_places=2,
        verbose_name="开仓均价"
    )
    TradeTime = models.CharField(
        db_column="TradeTime",
        max_length=19,
        verbose_name="成交时间"
    )
    TradeYTM = models.DecimalField(
        db_column="TradeYTM",
        max_digits=20,
        decimal_places=2,
        verbose_name="收益率"
    )

    class Meta:
        db_table = 't_backtrade'


# 快速回测分析结果表
class TBackReturn(models.Model):
    # ID = models.AutoField(
    #     db_column="ID",
    #     primary_key=True
    # )
    StgID = models.IntegerField(
        db_column="StgID",
        verbose_name="策略代码",
        primary_key=True
    )
    BatchID = models.IntegerField(
        db_column="BatchID",
        verbose_name="策略运行代码"
    )
    data = models.TextField(
        db_column="data",
        verbose_name="结果数据"
    )
    date = models.TextField(
        db_column="date",
        verbose_name="日期数据"
    )
    heatYear = models.TextField(
        db_column="heatYear",
        verbose_name="热图年份"
    )
    heatData = models.TextField(
        db_column="heatData",
        verbose_name="热图数据"
    )
    yearlyRetDate = models.TextField(
        db_column="yearlyRetDate",
        verbose_name="年化年份"
    )
    yearlyRetData = models.TextField(
        db_column="yearlyRetData",
        verbose_name="年化收益"
    )
    monthlyRetDistData = models.TextField(
        db_column="monthlyRetDistData",
        verbose_name="月度收益"
    )
    boxData = models.TextField(
        db_column="boxData",
        verbose_name="黑箱数据"
    )

    class Meta:
        db_table = "t_backreturn"
        unique_together = (("StgID", "BatchID"),)


# 快速回测分析指标表
class TBackSummary(models.Model):
    # ID = models.AutoField(
    #     db_column="ID",
    #     primary_key=True,
    # )
    StgID = models.IntegerField(
        db_column="StgID",
        verbose_name="策略代码",
        primary_key=True,
    )
    BatchID = models.IntegerField(
        db_column="BatchID",
        verbose_name="策略运行代码"
    )
    SimTime = models.CharField(
        db_column="SimTime",
        max_length=19,
        verbose_name="策略运行时间"
    )
    SimStartDate = models.CharField(
        db_column="SimStartDate",
        max_length=10,
        verbose_name="行情订阅开始日期"
    )
    SimEndDate = models.CharField(
        db_column="SimEndDate",
        max_length=10,
        verbose_name="行情订阅结束日期"
    )
    TotalDays = models.IntegerField(
        db_column="TotalDays",
        verbose_name="总天数"
    )
    PL = models.DecimalField(
        db_column="PL",
        verbose_name="单利",
        max_digits=20,
        decimal_places=2
    )
    MaxDrawndown = models.DecimalField(
        db_column="MaxDrawndown",
        verbose_name="最大回撤",
        max_digits=20,
        decimal_places=2
    )
    MaxDrawndownDays = models.IntegerField(
        db_column="MaxDrawndownDays",
        verbose_name="最大回撤天数"
    )
    SharpRatio = models.DecimalField(
        db_column="SharpRatio",
        verbose_name="夏普比率",
        max_digits=20,
        decimal_places=2
    )
    CalmarRatio = models.DecimalField(
        db_column="CalmarRatio",
        verbose_name="calmar比率",
        max_digits=20,
        decimal_places=2
    )
    WinDays = models.IntegerField(
        db_column="WinDays",
        verbose_name="盈利天数"
    )
    LossDays = models.IntegerField(
        db_column="LossDays",
        verbose_name="亏损天数"
    )
    WinDaysRate = models.DecimalField(
        db_column="WinDaysRate",
        verbose_name="日胜率",
        max_digits=20,
        decimal_places=2
    )
    TotalTrans = models.IntegerField(
        db_column="TotalTrans",
        verbose_name="交易信号总个数"
    )
    WinTrans = models.IntegerField(
        db_column="WinTrans",
        verbose_name="每个信号盈利个数"
    )
    LossTrans = models.IntegerField(
        db_column="LossTrans",
        verbose_name="每个信号亏损个数"
    )
    WinTransRate = models.DecimalField(
        db_column="WinTransRate",
        verbose_name="每个信号胜率",
        max_digits=20,
        decimal_places=2
    )
    AvgTradeProfit = models.DecimalField(
        db_column="AvgTradeProfit",
        verbose_name="每个信号盈利均值",
        max_digits=20,
        decimal_places=2
    )
    AvgDailyProfit = models.DecimalField(
        db_column="AvgDailyProfit",
        verbose_name="每日盈利均值",
        max_digits=20,
        decimal_places=2
    )
    BeginNetValue = models.DecimalField(
        db_column="BeginNetValue",
        verbose_name="初始净值",
        max_digits=20,
        decimal_places=2
    )
    EndNetValue = models.DecimalField(
        db_column="EndNetValue",
        verbose_name="回测结束净值",
        max_digits=20,
        decimal_places=2
    )
    SummaryScore = models.IntegerField(
        db_column="SummaryScore",
        verbose_name="综合分数"
    )
    AnnualRet = models.DecimalField(
        db_column="AnnualRet",
        verbose_name="年化收益率",
        max_digits=20,
        decimal_places=2
    )
    ProfitScore = models.IntegerField(
        db_column="ProfitScore",
        verbose_name="盈利分数"
    )
    CostPerfScore = models.IntegerField(
        db_column="CostPerfScore",
        verbose_name="成本分数"
    )
    StabScore = models.IntegerField(
        db_column="StabScore",
        verbose_name="稳定分数"
    )
    CapitalScore = models.IntegerField(
        db_column="CapitalScore",
        verbose_name="资本分数"
    )
    SensitivityScore = models.IntegerField(
        db_column="SensitivityScore",
        verbose_name="敏感分数"
    )
    AnnualVolatility = models.DecimalField(
        db_column="AnnualVolatility",
        verbose_name="年化波动率",
        max_digits=20,
        decimal_places=2
    )
    StabilityOfTimeseries = models.DecimalField(
        db_column="StabilityOfTimeseries",
        verbose_name="时间稳定性",
        max_digits=20,
        decimal_places=2
    )
    OmegaRatio = models.DecimalField(
        db_column="OmegaRatio",
        verbose_name="Omega比率",
        max_digits=20,
        decimal_places=2
    )
    SortinoRatio = models.DecimalField(
        db_column="SortinoRatio",
        verbose_name="索提诺比率",
        max_digits=20,
        decimal_places=2
    )
    Skew = models.DecimalField(
        db_column="Skew",
        verbose_name="偏移量",
        max_digits=20,
        decimal_places=2
    )
    Kurtosis = models.DecimalField(
        db_column="Kurtosis",
        verbose_name="峰值",
        max_digits=20,
        decimal_places=2
    )
    TailRatio = models.DecimalField(
        db_column="TailRatio",
        verbose_name="Tail比率",
        max_digits=20,
        decimal_places=2
    )
    CommonSenseRatio = models.DecimalField(
        db_column="CommonSenseRatio",
        verbose_name="一般比率",
        max_digits=20,
        decimal_places=2
    )
    CumReturnsFinal = models.DecimalField(
        db_column="CumReturnsFinal",
        verbose_name="回测收益",
        max_digits=20,
        decimal_places=2
    )

    class Meta:
        db_table = "t_backsummary"
        unique_together = (("StgID", "BatchID"),)


# 快速回测持仓表
class TBackposition(models.Model):
    # id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    stgid = models.IntegerField(db_column='StgID', primary_key=True)  # Field name made lowercase.
    batchid = models.IntegerField(db_column='BatchID')  # Field name made lowercase.
    accountid = models.CharField(db_column='AccountID', max_length=32)  # Field name made lowercase.
    symbol = models.CharField(db_column='Symbol', max_length=30)  # Field name made lowercase.
    tradingday = models.CharField(db_column='TradingDay', max_length=8)  # Field name made lowercase.
    exchid = models.CharField(db_column='ExchID', max_length=10)  # Field name made lowercase.
    closepx = models.FloatField(db_column='ClosePx')  # Field name made lowercase.
    buyqty = models.BigIntegerField(db_column='BuyQty')  # Field name made lowercase.
    buyopenpx = models.FloatField(db_column='BuyOpenPx')  # Field name made lowercase.
    buymargin = models.FloatField(db_column='BuyMargin')  # Field name made lowercase.
    buypnl = models.FloatField(db_column='BuyPNL')  # Field name made lowercase.
    sellqty = models.BigIntegerField(db_column='SellQty')  # Field name made lowercase.
    sellopenpx = models.FloatField(db_column='SellOpenPx')  # Field name made lowercase.
    sellmargin = models.FloatField(db_column='SellMargin')  # Field name made lowercase.
    sellpnl = models.FloatField(db_column='SellPNL')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 't_backposition'
        # unique_together = (('id', 'stgid', 'batchid', 'accountid', 'symbol', 'tradingday'),)
        unique_together = (('stgid', 'batchid', 'accountid', 'symbol', 'tradingday'),)
